﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

using SBPweb.RIA.Client.Contracts.Base;

using SBPweb.PROvibe.Client.Library.Base;

using SBPweb.PROvibe.Shared.Library.Components;
using SBPweb.PROvibe.Shared.Library.Layout;

namespace SBPweb.PROvibe.Client.Library.Models
{

    public abstract class ContentContextModelBase : ModelBase, IContentContextModel
    {

        #region Dependencies

        public static readonly DependencyProperty PageProperty = DependencyProperty.Register("Page", typeof(IPage), typeof(ContentContextModelBase), null);
        public static readonly DependencyProperty ApplicationProperty = DependencyProperty.Register("Application", typeof(IApplication), typeof(ContentContextModelBase), null);
        public static readonly DependencyProperty ModulesProperty = DependencyProperty.Register("Modules", typeof(ReadOnlyObservableCollection<IModule>), typeof(ContentContextModelBase), null);
        public static readonly DependencyProperty ServicesProperty = DependencyProperty.Register("Services", typeof(ReadOnlyObservableCollection<IService>), typeof(ContentContextModelBase), null);
        public static readonly DependencyProperty LayoutProperty = DependencyProperty.Register("Layout", typeof(LayoutModes), typeof(ContentContextModelBase), null);

        #endregion

        #region Properties

        public virtual ReadOnlyObservableCollection<IModule> Modules
        {
            get
            {
                return GetTypedValue<ReadOnlyObservableCollection<IModule>>(ModulesProperty);
            }
            protected set
            {
                SetTypedValue<ReadOnlyObservableCollection<IModule>>(ModulesProperty, value, "Modules");
            }
        }

        public virtual IApplication Application
        {
            get
            {
                return GetTypedValue<IApplication>(ApplicationProperty);
            }
            protected set
            {
                SetTypedValue<IApplication>(ApplicationProperty, value, "Application");
            }
        }

        public virtual IPage Page
        {
            get
            {
                return GetTypedValue<IPage>(PageProperty);
            }
            protected set
            {
                SetTypedValue<IPage>(PageProperty, value, "Page");
            }
        }

        public virtual ReadOnlyObservableCollection<IService> Services
        {
            get
            {
                return GetTypedValue<ReadOnlyObservableCollection<IService>>(ServicesProperty);
            }
            protected set
            {
                SetTypedValue<ReadOnlyObservableCollection<IService>>(ServicesProperty, value, "Services");
            }
        }

        public IClientModule CurrentModule
        {
            get;
            internal set;
        }

        public virtual LayoutModes Layout
        {
            get
            {
                return GetTypedValue<LayoutModes>(LayoutProperty);
            }
            protected set
            {
                SetTypedValue<LayoutModes>(LayoutProperty, value, "Layout");
            }
        }

        #endregion

        #region Methods

        internal void SetCurrentModuleByInstance(Type instanceType)
        {
            if (Modules == null)
            {
                return;
            }
            CurrentModule = Modules.WhereTypeIs<IClientModule>().FirstOrDefault(x => x.GetType().Assembly == instanceType.Assembly);
        }

        #endregion

    }

}